<!--
  @Description:新增编辑自定义页面
  @Author:zqy
  @Date:2022-12-15 14:17:26
  @Last Modified by:zqy
  @Last Modified time:2022-12-15 14:17:26
-->
<template>
  <el-dialog
    ref="formDlg"
    title="页面基础信息配置"
    :visible.sync="dialogShow"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :before-close="clearForm"
    width="500px"
    @close="closeDialog"
  >
    <div style="padding-right:30px">
      <el-form ref="refForm" :rules="formRules" :model="formData" size="mini" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="页面名称:" prop="name">
              <el-input v-model.trim="formData.name" maxlength="25" show-word-limit placeholder="请输入页面名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="类型:" prop="type">
              <el-select v-model="formData.type" :disabled="!!(detail && detail.id)">
                <el-option label="组合" value="combination" />
                <el-option label="商品" value="product" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="页面图标:">
              <div class="img-upload-container">
                <qup-img-uploader
                  :width="100"
                  :height="100"
                  mask
                  :image-url="cateImage.absoluteUrl"
                  :relative-url="cateImage.relativeUrl"
                  @upload-success="handleUploadSuccess"
                  @remove="handleDelImg"
                />
                <div class="icon-msg">建议图片尺寸大小100*100,不超过1M,支持png\jpg\jpeg</div>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div slot="footer">
      <el-button size="mini" @click="closeDialog">取消</el-button>
      <el-button size="mini" type="primary" @click="submitForm">保存</el-button>
      <el-button size="mini" type="primary" @click="submitForm('config')">保存并去配置楼层</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { savePageConf } from '@/api/definePageConfig'
import QupImgUploader from '@/components/imgUploader'
export default {
  name: 'DefinePageEdit',
  components: { QupImgUploader },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    detail: {
      type: [String, Object],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id: null,
        name: '',
        type: '',
        iconPicUrl: '',
        enableFlag: 1
      },
      formRules: {
        name: [
          { required: true, message: '请输入页面名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ]
      },
      cateImage: {
        absoluteUrl: '',
        relativeUrl: ''
      },
      submiting: false
    }
  },

  computed: {
    dlgTitle() {
      return this.formData && this.formData.id ? '编辑页面基础信息配置' : '新增页面基础信息配置'
    },
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  created() {
    if (this.detail && this.detail.id) {
      const data = this.detail
      const enableFlag = [0, '0', 1, '1'].includes(data.enableFlag) ? data.enableFlag : 1
      this.formData = {
        id: data.id,
        name: data.name,
        type: data.type,
        iconPicUrl: data.iconPicUrl,
        enableFlag
      }
      this.cateImage = {
        absoluteUrl: this.formatImageUrl(data.iconPicUrl),
        relativeUrl: data.iconPicUrl
      }
    }
  },

  methods: {
    closeDialog() {
      this.dialogShow = false
      this.clearForm()
    },
    clearForm(done) {
      this.formData = {
        id: null,
        name: '',
        type: '',
        iconPicUrl: '',
        enableFlag: 1
      }
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
      this.$refs.refForm && this.$refs.refForm.clearValidate()
      done && done()
    },
    submitForm(type) {
      if (this.submiting) return
      this.$refs['refForm'].validate(async(valid) => {
        if (valid) {
          const formData = Object.assign(this.formData)
          this.submiting = true
          await savePageConf(formData).then(res => {
            this.submiting = false
            if (res.success === 1) {
              this.$message({
                // message: res.msg ? res.msg : `${formData.id ? '编辑' : '新增'}保存成功`,
                message: `${formData.id ? '编辑' : '新增'}保存成功`,
                type: 'success'
              })
              this.closeDialog()
              let data = {
                id: (this.detail && this.detail.id) || res.data,
                name: formData.name,
                type: formData.type,
                iconPicUrl: formData.iconPicUrl
              }
              if (formData.id) {
                data = {
                  ...this.detail,
                  ...formData
                }
              }
              const result = {
                data,
                goConfig: type === 'config'
              }
              this.$emit('success', result)
            }
          }).catch(() => {
            this.submiting = false
          })
        }
      })
    },
    handleUploadSuccess(res, file, fileList) {
      this.formData.iconPicUrl = res.storeFileName
      this.cateImage = {
        absoluteUrl: res.url,
        relativeUrl: res.storeFileName
      }
    },
    handleDelImg() {
      this.formData.iconPicUrl = ''
      this.cateImage = {
        absoluteUrl: '',
        relativeUrl: ''
      }
    }
  }
}
</script>
<style lang='scss' scoped>
  .img-upload-container {
    .icon-msg{
      margin-top: 10px;
      color: #999;
    }
  }
</style>
